package com.example.administrator.zyfypt_422zwj.model;

import com.example.administrator.zyfypt_422zwj.bean.LoginBean;
import com.example.administrator.zyfypt_422zwj.iface.LoginIface;
import com.example.administrator.zyfypt_422zwj.iface.LoginListener;
import com.example.administrator.zyfypt_422zwj.service.LoginService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Administrator on 2017/5/2 0002.
 */

public class LoginModel implements LoginIface {

    private Retrofit retrofit;

    private String BASEURL = "http://amicool.neusoft.edu.cn/";

    public LoginModel(){
        retrofit = new Retrofit.Builder().baseUrl(BASEURL).addConverterFactory(GsonConverterFactory.create()).build();
    }

    @Override
    public void getResult(String username, String password, final LoginListener loginListener) {
        LoginService loginService = retrofit.create(LoginService.class);
        Call<LoginBean> call = loginService.login(username, password);
        call.enqueue(new Callback<LoginBean>() {
            @Override
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                if(response.isSuccessful()&&response!=null)
                {
                    loginListener.onResponse(response.body());
                }
                else
                    loginListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {
                loginListener.onFail(t.toString());
            }
        });
    }
}

